//
//  DispatchQueueUtils.swift
//
//  Created by Ink on 2024-03-15.
//

import Foundation

extension DispatchQueue {
    static var `default`: DispatchQueue { DispatchQueue.global(qos: .`default`) }
    static var userInteractive: DispatchQueue { DispatchQueue.global(qos: .userInteractive) }
    static var userInitiated: DispatchQueue { DispatchQueue.global(qos: .userInitiated) }
    static var utility: DispatchQueue { DispatchQueue.global(qos: .utility) }
    static var background: DispatchQueue { DispatchQueue.global(qos: .background) }

    func after(_ delay: TimeInterval = 0, execute work: @escaping @convention(block) () -> Void) {
        asyncAfter(deadline: .now() + delay, execute: work)
    }

    private static var _onceTracker = [String]()
    public class func once(_ token: String, block:()->Void) {
        objc_sync_enter(self)
        defer { objc_sync_exit(self) }

        if _onceTracker.contains(token) {
            return
        }
        _onceTracker.append(token)
        block()
    }
}
